package com.onlyphoto.onlymall.utils;

import android.content.Context;
import android.os.Build;
import android.webkit.CookieManager;

import com.onlyphoto.onlymall.MyApplication;
import com.onlyphoto.onlymall.bean.response.UserInfoBean;

/**
 * Created by gaochujia on 2019-6-27.
 * Cookie 工具类
 */

public class CookieManagerUtil {

    private static String TAG = "CookieManagerUtil";

    /**
     * 设置cookie
     *
     * @param context 上下文
     */
    public static void setupCookie(Context context, UserInfoBean userInfo) {
        /*if (null == userInfo) {
            return;
        }
        clearCookie(context);

        CookieManager cm = CookieManager.getInstance();
        String cookie = "SESSIONID=" + userInfo.getSessionID();
        List<String> cookieList = new Gson().fromJson(SharePreferenceUtil.getInstance(context).getData(ConfigUtil.KEY_COOKIE_LIST), new TypeToken<List<String>>() {
        }.getType());
        if (!CollectionUtils.isEmpty(cookieList)) {
            for (String str : cookieList) {
                cm.setCookie(str, cookie);
            }
        }
        cm.setCookie("http://192.168.8.101/", cookie);
        cm.setCookie("http://121.42.12.69/", cookie);
        cm.setCookie(".kdqugou.com/", cookie);
        cm.setCookie(".koudaikj.com", cookie);
        cm.setCookie(".koudailc.com", "domain=.koudailc.com");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().flush();
        }
        CookieSyncManager.getInstance().sync();*/
    }

    /**
     * 清除Cookie
     *
     * @param context 上下文
     */
    public static void clearCookie(Context context) {
        SharePreferenceUtil.getInstance(MyApplication.Companion.getApp()).setToken("");
        SharePreferenceUtil.getInstance(MyApplication.Companion.getApp()).setUserName("");
        //清除cookie
        CookieManager cm = CookieManager.getInstance();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cm.removeAllCookies(aBoolean -> {
                LogUtil.log(TAG, "所有Cookie已经移除");
            });
        }
    }
}
